package com.linwen.javase.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import static java.sql.DriverManager.*;

//insert
public class JdbcDemoinsert {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybaties?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true", "root", "123456789");
            String insertSql = "insert into user(username,birthday,sex,address)values('赵六','2020-06-02','男','深圳')";
            statement = connection.createStatement();
            int num = statement.executeUpdate(insertSql);
            if (num > 0) {
                System.out.println("insert success");
            } else {
                System.out.println("insert faile");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("注册驱动失败");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            System.out.println("连接数据库失败");
        } finally {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

    }
}
